import re
from coin.exchange.base.kr_rest.product import NativeProduct
from coin.exchange.uniswap_v2.kr_rest.constants import quote_currency_list
from coin.exchange.uniswap_v2.kr_rest.currency import UniswapCurrency


class UniswapProduct(NativeProduct):
  _exchange = "Uniswap"
  NativeCurrencyType = UniswapCurrency

  def __init__(self, base, quote):
    assert base != quote
    assert quote.currency in quote_currency_list, quote
    super().__init__(
        base, quote, "%s-%s" % (base.native_currency, quote.native_currency))

  @classmethod
  def FromStrProductImpl(cls, product, current_datetime):
    assert type(product) == str, type(product)
    assert any(c in product for c in "-"), product
    splitted = re.split('-', product)
    assert len(splitted) == 2
    return UniswapProduct(
        UniswapCurrency.FromStrCurrency(splitted[0], current_datetime),
        UniswapCurrency.FromStrCurrency(splitted[1], current_datetime))

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert type(product) == str
    return cls.FromStrProductImpl(product, current_datetime)
